package com.hjc.controller;

import com.hjc.entity.*;
import com.hjc.page.Page;
import com.hjc.service.*;
import com.hjc.utils.TokenUtils;
import com.hjc.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private ProduceTypeService produceTypeService;
    @Autowired
    private ProductService productService;
    @Autowired
    private TokenUtils tokenUtils;

    @RequestMapping("/store-list")
    public Result findAllStore() {
        List<Store> storeList = storeService.findAllStore();
        return Result.ok(storeList);
    }

    @RequestMapping("/brand-list")
    public Result findAllBrand() {
        List<Brand> brandList = brandService.findAllBrand();
        return Result.ok(brandList);
    }

    @RequestMapping("/category-tree")
    public Result findAllCategory() {
        List<ProductType> categoryTree = produceTypeService.findAllProductType();
        return Result.ok(categoryTree);
    }

    @RequestMapping("/supply-list")
    public Result findAllSupply() {
        List<Supply> supplyList = supplyService.findAllSupply();
        return Result.ok(supplyList);
    }

    @RequestMapping("/place-list")
    public Result findAllPlace() {
        List<Place> placeList = placeService.findAllPlace();
        return Result.ok(placeList);
    }

    @RequestMapping("/unit-list")
    public Result findAllUnit() {
        List<Unit> unitList = unitService.findAllUnit();
        return Result.ok(unitList);
    }

//    http://localhost:3000/api/product/product-page-list?
//    storeId=1&productName=&brandName=&typeName=&supplyName=&placeName=&upDownState=&isOverDate=&
//    pageSize=5&pageNum=1&totalNum=0
    @RequestMapping("/product-page-list")
    public Result ProductList(Page page,Product product) {
        page = productService.findAllProductList(page, product);
        return Result.ok(page);
    }

    /**
     * 将配置文件的file.upload-path属性值注入给控制器的uploadPath属性,
     * 其为图片上传到项目的目录路径(类路径classes即resources下的static/img/upload);
     */
    @Value("${file.upload-path}")
    private String uploadPath;
    /**
     * 上传图片的url接口/product/img-upload
     *
     * 参数MultipartFile file对象封装了上传的图片;
     *
     * @CrossOrigin表示该url接口允许跨域请求;
     */
    @CrossOrigin
    @PostMapping("/img-upload")
    public Result uploadImg(MultipartFile file){

        try {
            //拿到图片上传到的目录(类路径classes下的static/img/upload)的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片保存到的磁盘路径
            String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename();
            //保存图片
            file.transferTo(new File(fileUploadPath));
            //成功响应
            return Result.ok("图片上传成功！");
        } catch (IOException e) {
            //失败响应
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

    // http://localhost:3000/api/product/product-add
    @RequestMapping("product-add")
    public Result addProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setCreateBy(currentUser.getUserId());
        return productService.addProduct(product);
    }

    //http://localhost:3000/api/product/state-change
    @RequestMapping("state-change")
    public Result setUpDownState(@RequestBody Product product) {
        return productService.setUpDownState(product.getProductId(),product.getUpDownState());
    }

//    http://localhost:3000/api/product/product-delete/34
    @RequestMapping("/product-delete/{productId}")
    public Result removeProductByProductId(@PathVariable Integer productId) {
        return productService.removeProductByProductId(productId);
    }

//    修改商品信息http://localhost:3000/api/product/product-update
    @RequestMapping("/product-update")
    public Result setProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setUpdateBy(currentUser.getUserId());
        return productService.setProductById(product);
    }

}
